//package cn.xinfei.xdecision.rabbit;
//
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Slf4j
//@Configuration
//@ConditionalOnProperty( name = "xdecision.rabbit.enable")
//public class RabbitConfig {
//    //资源owner账户 ID 信息
//    private static final String INSTANCE_ID ="1058333820173956";
//    @Autowired
//    private RabbitProperties rabbitProperties;
//    @Bean
//    @ConditionalOnProperty( name = "xdecision.rabbit.enable")
//    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate();
//        rabbitTemplate.setConnectionFactory(connectionFactory);
//        /*
//         * 当mandatory标志位设置为true时
//         * 如果exchange根据自身类型和消息routingKey无法找到一个合适的queue存储消息
//         * 那么broker会调用basic.return方法将消息返还给生产者
//         * 当mandatory设置为false时，出现上述情况broker会直接将消息丢弃
//         */
//        rabbitTemplate.setMandatory(true);
////        // 配置ReturnCallback
//        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
//            // 记录日志
//            try{
//                log.error("消息发送到队列失败，响应码：{}, 失败原因：{}, 交换机: {}, 路由key：{}, 消息: {}", replyCode, replyText, exchange, routingKey, message.toString());
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//            // 如果有需要的话，重发消息
//        });
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//                log.error("消息投递到交换机失败，相关数据：{}, 确认情况ack：{}, 原因: {}",correlationData, ack, cause);
//            }
//        });
//        return rabbitTemplate;
//    }
//
//    @Bean
//    @ConditionalOnProperty( name = "xdecision.rabbit.enable")
//    public SimpleMessageListenerContainer signinMessageContainer(ConnectionFactory connectionFactory) {
//        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
//        return container;
//    }
//
//    @Bean
//    @ConditionalOnProperty( name = "xdecision.rabbit.enable")
//    public ConnectionFactory getConnectionFactory(@Autowired RabbitProperties rabbitProperties) {
//        com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory =  new com.rabbitmq.client.ConnectionFactory();
//        rabbitConnectionFactory.setHost(rabbitProperties.getHost());
//        rabbitConnectionFactory.setPort(rabbitProperties.getPort());
//        rabbitConnectionFactory.setVirtualHost(rabbitProperties.getVirtualHost());
//
//        AliyunCredentialsProvider credentialsProvider = new AliyunCredentialsProvider( rabbitProperties.getUsername(), rabbitProperties.getPassword(), INSTANCE_ID);
//        rabbitConnectionFactory.setCredentialsProvider(credentialsProvider);
//
//        rabbitConnectionFactory.setAutomaticRecoveryEnabled(false);//禁用rabbitMq自动恢复连接设置，使用Spring AMQP的恢复机制
//        rabbitConnectionFactory.setNetworkRecoveryInterval(5000);
//        ConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitConnectionFactory);
//        ((CachingConnectionFactory)connectionFactory).setPublisherReturns(rabbitProperties.isPublisherReturns());
//        return connectionFactory;
//    }
//
//}